﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Badhshala.Models;

namespace Badhshala.Controllers
{
    public class HomeController : BaseController
    {
        [Authorize]
        public ActionResult Jackpot()
        {
            SlotMachineDBEntities ctx = new SlotMachineDBEntities();
            AppUser user = ctx.AppUsers.FirstOrDefault(u => u.FacebookId == FacebookId);

            return View(user);
        }

        [HttpPost]
        public ActionResult PostResult(Result result)
        {
            ScoreChecker scoreChecker = new ScoreChecker();
            result.CurrentWin = scoreChecker.CheckScore(result);

            SlotMachineDBEntities ctx = new SlotMachineDBEntities();
            AppUser user = ctx.AppUsers.First(u => u.FacebookId == FacebookId);
            user.Credits += result.CurrentWin;
            user.TimesPlayed++;

            ctx.SaveChanges();

            result.FinalCredit = user.Credits;
            return this.Json(result);
        }

        [HttpPost]
        public ActionResult SaveAppRequestsMade(int totalRequests)
        {
            SlotMachineDBEntities ctx = new SlotMachineDBEntities();
            AppUser user = ctx.AppUsers.First(u => u.FacebookId == FacebookId);
            user.Credits += GlobalConstants.CreditForEachFriendRequest * totalRequests;

            ctx.SaveChanges();

            Result result = new Result();
            result.CurrentWin = GlobalConstants.CreditForEachFriendRequest * totalRequests;
            result.FinalCredit = user.Credits;

            return this.Json(result);
        }

        [HttpPost]
        public ActionResult SavePostToWall()
        {
            SlotMachineDBEntities ctx = new SlotMachineDBEntities();
            AppUser user = ctx.AppUsers.First(u => u.FacebookId == FacebookId);
            user.Credits += GlobalConstants.CreditForWallPost;

            ctx.SaveChanges();

            Result result = new Result();
            result.CurrentWin = GlobalConstants.CreditForWallPost;
            result.FinalCredit = user.Credits;

            return this.Json(result);
        }

        [HttpPost]
        public ActionResult EnterLottery()
        {
            SlotMachineDBEntities ctx = new SlotMachineDBEntities();
            AppUser user = ctx.AppUsers.First(u => u.FacebookId == FacebookId);

            if (user.Credits >= GlobalConstants.CreditToEnterLottery)
            {
                user.Credits -= GlobalConstants.CreditToEnterLottery;

                LotteryParticipant participant = new LotteryParticipant(FacebookId, DateTime.Now);
                ctx.LotteryParticipants.AddObject(participant);

                ctx.SaveChanges();

                Result result = new Result();
                result.CurrentWin = 0;
                result.FinalCredit = user.Credits;

                return this.Json(result);
            }
            else
            {
                return null;
            }
        }
    }
}
